package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.CustomEvent;
import com.qire.common.constant.DeviceConfigInfo;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.common.utils.NoticeEventBus;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.dal.IMessageDataDAL;
import com.qire.manhua.model.vo.MessagePushStateVO;
import com.qire.manhua.model.vo.OkResponseVO;

public class MessageSetupService extends DataService {

    private final IMessageDataDAL messageDataDAL = WarehouseFactory.create(this, IMessageDataDAL.class);

    /**
     * 更新推送状态到服务器
     */
    public void updateMessagePushState() {
        messageDataDAL.togglePushStateInfo(DeviceConfigInfo.messagePushEnable(), DeviceConfigInfo.comicUpdatePushEnable(),
                DeviceConfigInfo.recommendPushEnable(), DeviceConfigInfo.commentPushEnable());
    }
    @StrategyAnnotation(event = Url.PushStateToggle)
    public void togglePushStateInfo(OkResponseVO dataVO, boolean isCache) {
        if(ObjectUtil.isNull(dataVO) || !dataVO.isOK()) {
            ToastHelper.showSnackBar("消息推送切换失败", Prompt.ERROR);
        } else {
            ToastHelper.showSnackBar("消息推送切换成功", Prompt.SUCCESS);
        }
        syncPushStateInfoByService();
    }

    /**
     * 切换完成后,通过服务器同步推送状态信息
     */
    public void syncPushStateInfoByService() {
        messageDataDAL.fetchPushStateInfo();
    }
    @StrategyAnnotation(event = Url.PushStateInfo)
    public void fetchPushStateInfoApiCallback(MessagePushStateVO dataVO, boolean isCache) {
        if(ObjectUtil.nonNull(dataVO) && ObjectUtil.nonNull(dataVO.kaiguan)) {

            DeviceConfigInfo.updateComicUpdatePushEnable(dataVO.kaiguan.comicUpdatePushEnable());
            DeviceConfigInfo.updateRecommendPushEnable(dataVO.kaiguan.recommendPushEnable());
            DeviceConfigInfo.updateCommentPushEnable(dataVO.kaiguan.commentPushEnable());

            NoticeEventBus.NOTICE.sendEvent(CustomEvent.SYNC_PUSH_STATE_INFO);
        }
    }

}
